﻿using App.Common;
using QxFramework.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class StartUI : UIBase
{
    Transform _backGround;
    Transform _singleBtn;
    Transform _multipleBtn;
    Transform _quitBtn;
    Vector3 _groundPos;
    Vector3 _singleBtnPos;
    Vector3 _multipleBtnPos;
    Vector3 _quitBtnPos;

    public override void OnDisplay(object args)
    {
        base.OnDisplay(args);
        CollectObject();
        InitUI();
        _gos["StartSingleBtn"].GetComponent<Button>().onClick.RemoveAllListeners();
        _gos["StartSingleBtn"].GetComponent<Button>().onClick.AddListener(()=> {
            Data.Instance.PlayersCount = 1;
            StartGame();
        });
        _gos["StartMultipleBtn"].GetComponent<Button>().onClick.RemoveAllListeners();
        _gos["StartMultipleBtn"].GetComponent<Button>().onClick.AddListener(() => {
            Data.Instance.PlayersCount = 2;
            StartGame();
        });
        _gos["QuitBtn"].GetComponent<Button>().onClick.RemoveAllListeners();
        _gos["QuitBtn"].GetComponent<Button>().onClick.AddListener(() =>
        {
#if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
#else
        Application.Quit();
#endif
        });
    }
    private void InitUI()
    {
        _backGround = gameObject.transform.Find("TitleBackGround");
        _singleBtn = gameObject.transform.Find("StartSingleBtn");
        _multipleBtn = gameObject.transform.Find("StartMultipleBtn");
        _quitBtn = gameObject.transform.Find("QuitBtn");
        _groundPos = _backGround.position;
        _singleBtnPos = _singleBtn.position;
        _multipleBtnPos = _multipleBtn.position;
        _quitBtnPos = _quitBtn.position;
    }
    public void Update()
    {
        var pos = InputManager.Instance.GetTarget(0);
        var square = pos.x * pos.x + pos.y * pos.y;
        _backGround.position = new Vector3(_groundPos.x - (pos.x / Mathf.Sqrt(square)) * 0.03f, _groundPos.y - (pos.y / Mathf.Sqrt(square)) * 0.03f, _groundPos.z);
        _singleBtn.position = new Vector3(_singleBtnPos.x - (pos.x / Mathf.Sqrt(square)) * 5f, _singleBtnPos.y - (pos.y / Mathf.Sqrt(square)) * 5f, _singleBtnPos.z);
        _multipleBtn.position = new Vector3(_multipleBtnPos.x - (pos.x / Mathf.Sqrt(square)) * 5f, _multipleBtnPos.y - (pos.y / Mathf.Sqrt(square)) * 5f, _multipleBtnPos.z);
        _quitBtn.position = new Vector3(_quitBtnPos.x - (pos.x / Mathf.Sqrt(square)) * 5f, _quitBtnPos.y - (pos.y / Mathf.Sqrt(square)) * 5f, _quitBtnPos.z);
    }
    public void StartGame()
    {
        StartCoroutine(Init());
//         UIManager.Instance.Open("BlackMask", 2, args: new BlackMask.BlackMaskArg
//        (() => {
//            GameMgr.Instance.InitModules();
//            GameMgr.Get<IMapManager>().InitMapFile();
//            GameMgr.Get<IMapManager>().InitMapData(Data.Instance.StartLevel);
//            GameMgr.Get<IMapManager>().InitDoorsAndCorridors();
//            GameMgr.Get<IMapManager>().InitEdgeWall();
//            GameMgr.Get<IMissionManager>().InitMission(Data.Instance.StartLevel);
//            UIManager.Instance.GetUI("BlackMask").GetComponent<BlackMask>().CloseUI();
//        }));
//         UIManager.Instance.Close(this);
    }
    private IEnumerator Init()
    {
        var mask = UIManager.Instance.Open("BlackMask", 2);
        yield return new WaitForSeconds(0.5f);
        mask.transform.Find("Loading").GetComponent<Text>().fontSize = 20;
        mask.transform.Find("Loading").GetComponent<Text>().text = "正在初始化所有模块...";
        GameMgr.Instance.InitModules();
        yield return new WaitForSeconds(0.1f);
        mask.transform.Find("Loading").GetComponent<Text>().text = "正在加载地图...";
        GameMgr.Get<IMapManager>().InitMapFile();
        yield return new WaitForSeconds(0.1f);
        mask.transform.Find("Loading").GetComponent<Text>().text = "正在生成地砖...";
        GameMgr.Get<IMapManager>().InitMapData(Data.Instance.StartLevel);
        yield return new WaitForSeconds(0.1f);
        mask.transform.Find("Loading").GetComponent<Text>().text = "正在生成走廊...";
        GameMgr.Get<IMapManager>().InitDoorsAndCorridors();
        yield return new WaitForSeconds(0.1f);
        mask.transform.Find("Loading").GetComponent<Text>().text = "正在生成墙体...";
        GameMgr.Get<IMapManager>().InitEdgeWall();
        yield return new WaitForSeconds(0.3f);
        mask.transform.Find("Loading").GetComponent<Text>().text = "正在进入游戏...";
        yield return new WaitForSeconds(0.5f);
        GameMgr.Get<IMissionManager>().InitMission(Data.Instance.StartLevel);
        UIManager.Instance.Close("BlackMask");
        UIManager.Instance.Close(this);
    }
}
